/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.setup.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.yulichang.repository.JoinCrudRepository;
import com.iwindplus.boot.domain.constant.CommonConstant;
import com.iwindplus.boot.domain.enums.BizCodeEnum;
import com.iwindplus.boot.domain.enums.EnableStatusEnum;
import com.iwindplus.boot.domain.exception.BizException;
import com.iwindplus.mgt.domain.enums.MgtCodeEnum;
import com.iwindplus.setup.domain.dto.OssTplEditDTO;
import com.iwindplus.setup.domain.dto.OssTplSaveDTO;
import com.iwindplus.setup.domain.dto.OssTplSearchDTO;
import com.iwindplus.setup.domain.enums.SetupCodeEnum;
import com.iwindplus.setup.domain.vo.OssTplPageVO;
import com.iwindplus.setup.domain.vo.OssTplVO;
import com.iwindplus.setup.server.mapper.OssTplMapper;
import com.iwindplus.setup.server.model.OssTplDO;
import com.iwindplus.setup.server.service.OssTplService;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import net.dreamlu.mica.core.utils.StringUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 对象存储模板业务层接口实现类.
 *
 * @author zengdegui
 * @since 2018/9/1
 */
@Service
@CacheConfig(cacheNames = {"ossTpl"})
@Transactional(rollbackFor = Exception.class)
public class OssTplServiceImpl extends JoinCrudRepository<OssTplMapper, OssTplDO> implements OssTplService {

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(OssTplSaveDTO entity) {
        this.getNameIsExist(entity.getName().trim());
        this.getBucketNameIsExist(entity.getBucketName().trim());

        entity.setStatus(EnableStatusEnum.ENABLE);
        entity.setBuildInFlag(Boolean.FALSE);
        String code = StringUtil.getUUID();
        entity.setCode(code);
        final OssTplDO model = BeanUtil.copyProperties(entity, OssTplDO.class);
        super.save(model);
        entity.setId(model.getId());
        return Boolean.TRUE;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean removeByIds(List<Long> ids) {
        List<OssTplDO> list = super.listByIds(ids);
        if (Objects.isNull(list)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        boolean match = list.stream().anyMatch(OssTplDO::getBuildInFlag);
        if (Boolean.TRUE.equals(match)) {
            throw new BizException(BizCodeEnum.HAS_BUILD_IN_DATA);
        }
        super.removeByIds(ids);
        return Boolean.TRUE;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean edit(OssTplEditDTO entity) {
        // 编辑
        OssTplDO data = super.getById(entity.getId());
        if (Objects.isNull(data)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        if (Boolean.TRUE.equals(data.getBuildInFlag())) {
            throw new BizException(BizCodeEnum.BUILD_IN_DATA_NOT_OPERATE);
        }
        if (CharSequenceUtil.isNotBlank(entity.getName()) && !CharSequenceUtil.equals(data.getName(), entity.getName().trim())) {
            this.getNameIsExist(entity.getName().trim());
        }
        if (CharSequenceUtil.isNotBlank(entity.getCode()) && !CharSequenceUtil.equals(data.getCode(), entity.getCode().trim())) {
            this.getCodeIsExist(entity.getCode().trim());
        }
        if (CharSequenceUtil.isNotBlank(entity.getBucketName()) && !CharSequenceUtil.equals(data.getBucketName(), entity.getBucketName().trim())) {
            this.getBucketNameIsExist(entity.getBucketName().trim());
        }
        if (Objects.isNull(entity.getVersion())) {
            entity.setVersion(data.getVersion());
        }
        final OssTplDO model = BeanUtil.copyProperties(entity, OssTplDO.class);
        super.updateById(model);
        return Boolean.TRUE;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean editStatus(Long id, EnableStatusEnum status) {
        OssTplDO data = super.getById(id);
        if (Objects.isNull(data)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        if (Boolean.TRUE.equals(data.getBuildInFlag())) {
            throw new BizException(BizCodeEnum.BUILD_IN_DATA_NOT_OPERATE);
        }
        if (status.equals(data.getStatus())) {
            throw new BizException(BizCodeEnum.ALREADY_OPERATED);
        }
        OssTplDO entity = new OssTplDO();
        entity.setId(id);
        entity.setStatus(status);
        entity.setVersion(data.getVersion());
        super.updateById(entity);
        return Boolean.TRUE;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean editBuildIn(Long id, Boolean buildInFlag) {
        OssTplDO data = super.getById(id);
        if (Objects.isNull(data)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        if (buildInFlag.equals(data.getBuildInFlag())) {
            throw new BizException(BizCodeEnum.ALREADY_OPERATED);
        }
        OssTplDO param = new OssTplDO();
        param.setId(id);
        param.setBuildInFlag(buildInFlag);
        param.setVersion(data.getVersion());
        super.updateById(param);
        return Boolean.TRUE;
    }

    @Override
    public IPage<OssTplPageVO> page(PageDTO<OssTplDO> page, OssTplSearchDTO entity) {
        page.setOptimizeCountSql(Boolean.FALSE);
        page.setOptimizeJoinOfCountSql(Boolean.FALSE);
        final LambdaQueryWrapper<OssTplDO> queryWrapper = Wrappers.lambdaQuery(OssTplDO.class);
        if (Objects.nonNull(entity.getStatus())) {
            queryWrapper.eq(OssTplDO::getStatus, entity.getStatus());
        }
        if (CharSequenceUtil.isNotBlank(entity.getCode())) {
            queryWrapper.eq(OssTplDO::getCode, entity.getCode().trim());
        }
        if (CharSequenceUtil.isNotBlank(entity.getName())) {
            queryWrapper.eq(OssTplDO::getName, entity.getName().trim());
        }
        if (CharSequenceUtil.isNotBlank(entity.getBucketName())) {
            queryWrapper.eq(OssTplDO::getBucketName, entity.getBucketName().trim());
        }
        // 排序
        List<OrderItem> orders = page.getOrders();
        if (CollUtil.isEmpty(orders)) {
            orders = new ArrayList<>(10);
            OrderItem item = OrderItem.desc(CommonConstant.DbConstant.MODIFIED_TIME);
            orders.add(item);
        }
        orders.forEach(order -> {
            String column = order.getColumn();
            String underline = CharSequenceUtil.toUnderlineCase(column);
            order.setColumn(underline);
        });
        page.setOrders(orders);
        queryWrapper.select(OssTplDO::getId, OssTplDO::getCreatedTime, OssTplDO::getCreatedBy, OssTplDO::getModifiedTime,
            OssTplDO::getModifiedBy, OssTplDO::getBuildInFlag, OssTplDO::getVersion, OssTplDO::getRemark, OssTplDO::getStatus,
            OssTplDO::getCode, OssTplDO::getName, OssTplDO::getBucketName
        );
        final PageDTO<OssTplDO> modelPage = super.page(page, queryWrapper);
        return modelPage.convert(model -> BeanUtil.copyProperties(model, OssTplPageVO.class));
    }

    @Cacheable(key = "#root.methodName + '_' + #p0", condition = "#p0 != null", unless = "#result == null")
    @Override
    public OssTplVO getByCode(String code) {
        OssTplDO data = super.getOne(Wrappers.lambdaQuery(OssTplDO.class)
            .eq(OssTplDO::getCode, code.trim()));
        if (Objects.isNull(data)) {
            throw new BizException(SetupCodeEnum.OSS_TEMPLATE_NOT_EXIST);
        }
        if (EnableStatusEnum.DISABLE == data.getStatus()) {
            throw new BizException(SetupCodeEnum.OSS_TEMPLATE_DISABLED);
        } else if (EnableStatusEnum.LOCKED == data.getStatus()) {
            throw new BizException(SetupCodeEnum.OSS_TEMPLATE_LOCKED);
        }
        final OssTplVO result = BeanUtil.copyProperties(data, OssTplVO.class);
        return result;
    }

    @Cacheable(key = "#root.methodName + '_' + #p0", condition = "#p0 != null", unless = "#result == null")
    @Override
    public OssTplVO getDetail(Long id) {
        OssTplDO data = super.getById(id);
        if (Objects.isNull(data)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        final OssTplVO result = BeanUtil.copyProperties(data, OssTplVO.class);
        return result;
    }

    private void getNameIsExist(String name) {
        boolean result = SqlHelper.retBool(super.count(Wrappers.lambdaQuery(OssTplDO.class)
            .eq(OssTplDO::getName, name)));
        if (Boolean.TRUE.equals(result)) {
            throw new BizException(MgtCodeEnum.NAME_EXIST);
        }
    }

    private void getCodeIsExist(String code) {
        boolean result = SqlHelper.retBool(super.count(Wrappers.lambdaQuery(OssTplDO.class)
            .eq(OssTplDO::getCode, code)));
        if (Boolean.TRUE.equals(result)) {
            throw new BizException(MgtCodeEnum.CODE_EXIST);
        }
    }

    private void getBucketNameIsExist(String bucketName) {
        boolean result = SqlHelper.retBool(super.count(Wrappers.lambdaQuery(OssTplDO.class)
            .eq(OssTplDO::getBucketName, bucketName.trim())));
        if (Boolean.TRUE.equals(result)) {
            throw new BizException(SetupCodeEnum.BUCKET_NAME_EXIST);
        }
    }
}
